package com.derek.omshopping.ui.home.model;

import com.derek.omshopping.bean.home.HomeIndexListAllBean;
import com.derek.omshopping.bean.home.HomeMainTypeBean;
import com.derek.omshopping.bean.home.HomeVideoBean;
import com.derek.omshopping.config.UrlConfig;
import com.derek.omshopping.http.home.IndexListRetrofitService;
import com.derek.omshopping.http.home.MainRetrofitService;
import com.derek.omshopping.http.home.VideoRetrofitService;
import com.derek.omshopping.ui.home.contract.FragmentMainTypeListener;
import com.derek.omshopping.ui.home.contract.HomeIndexListener;
import com.derek.omshopping.ui.home.contract.HomeVideoListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by duyb on 2016-10-07.
 * Desc:HomeFragmentBiz具体实现类 主要用于访问网络数据、访问数据库等
 */

public class FragmentMainTypeBizImpl implements IFragmentMainTypeBiz {
    List<HomeIndexListAllBean> list = new ArrayList();

    @Override
    public void LoadFragmentMainTypeData(final FragmentMainTypeListener loadNewsListener) {
        //使用Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConfig.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<List<HomeMainTypeBean>> call = retrofit.create(MainRetrofitService.class).getAllData();
        call.enqueue(new Callback<List<HomeMainTypeBean>>() {
            @Override
            public void onResponse(Call<List<HomeMainTypeBean>> call, Response<List<HomeMainTypeBean>> response) {
                loadNewsListener.onSuccess(response.body());

            }

            @Override
            public void onFailure(Call<List<HomeMainTypeBean>> call, Throwable t) {
                loadNewsListener.onFailure((Exception) t);
            }
        });


    }

    //加载首页视屏播放数据
    @Override
    public void LoadHomeVideoData(final HomeVideoListener loadListener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConfig.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        Call<List<HomeVideoBean>> call = retrofit.create(VideoRetrofitService.class).getVideoData();
        call.enqueue(new Callback<List<HomeVideoBean>>() {
            @Override
            public void onResponse(Call<List<HomeVideoBean>> call, Response<List<HomeVideoBean>> response) {
                loadListener.onSuccess(response.body());
            }

            @Override
            public void onFailure(Call<List<HomeVideoBean>> call, Throwable t) {
                loadListener.onFailure((Exception) t);
            }
        });


    }

    //首页列表详情
    @Override
    public void LoadHomeIndexData(final HomeIndexListener loadIndexListener) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConfig.BASEURL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        Call<String> call = retrofit.create(IndexListRetrofitService.class).getIndexListData();
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                try {
                    JSONObject object = new JSONObject(response.body());
                    JSONArray array = object.getJSONArray("setList");
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject obj = array.getJSONObject(i);
                        JSONArray arr = obj.getJSONArray("contentList");
                        for (int j = 0; j < arr.length(); j++) {
                            JSONObject ojb1 = arr.getJSONObject(j);
                            String item_image = ojb1.getString("item_image");
                            String brand_name = ojb1.getString("brand_name");
                            String mobile_tag_txt = ojb1.getString("item_name");
                            String connect_tgt_addr = ojb1.getString("connect_tgt_addr");
                            HomeIndexListAllBean bean = new HomeIndexListAllBean(brand_name, item_image, mobile_tag_txt,connect_tgt_addr);
                            list.add(bean);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
               // Log.e("LISTAAA_>",list.size()+""); 50
                loadIndexListener.onSuccess(list);
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                loadIndexListener.onFailure((Exception) t);
            }
        });
    }
}
